// mircoApp/business-district/index/index.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
var location = require("../../../mybehaviors/location/location");

var app = getApp();
Page({
  behaviors: [skinBehavior,location],
  /**
   * 页面的初始数据
   */
  data: {
    pageTitle: '--',
    integral: '',
    info: {},

    addInviterPop: false,
    business_invite_code: "",
    addCDKeyPop: false,
    cdkey_no: "",
    cdkey_pwd: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  async getData() {
    let urlStr = app.getNetAddresss('plugin.business-district.frontend.controllers.index.index');
    let response = await app._postNetWork({
      url: urlStr,
      data: {}
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);

    wx.setNavigationBarTitle({
      title: data.plugin_name
    });
    this.setData({
      info: data
    });
    wx.getStorage({
      key: 'yz_basic_info',
      success: (res)=> {
        this.setData({
          plugin_setting: res.data.plugin_setting
        });
      }
    })
  },
  async gotoCard(){
    this.isLock = true;
    if(!this.isLock) return;

    let urlStr = app.getNetAddresss('plugin.business-district.frontend.controllers.order.create');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        business_invite_code: this.data.business_invite_code,
        business_city: this.city,
        business_code: this.business_code
      }
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);

    this.isLock = false;

    wx.navigateTo({
      url: '/packageD/buy/orderPay/orderPay?order_id=' + data.order_ids,
    })
  },
  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = "";
    const { url, ...otherParam } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== "") _Param += key + "=" + otherParam[key] + "&";
    });
    if (url === "") return;
    wx.navigateTo({
      url: url + "?" + _Param.slice(0, -1),
    });
  },
  onClickVal(e){
    let {name,val} = e.currentTarget.dataset;
    this.setData({
      [name]: val
    });
  },
  onCopy(){
    wx.setClipboardData({
      //准备复制的数据
      data: this.data.info.member.invite_code,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    });
  },

  beforeBuyCard() {
    this.data.business_invite_code = "";
    if (!this.data.info.member.parent_id) {
      // 没有有上级的购买显示弹窗
      this.setData({
        addInviterPop: true
      })
      return;
    }
    this.getLocationCard();
  },

  getLocationCard() {
    this._getLocation((mapdata, e) => {
      console.log(mapdata,e);
      this.city = mapdata.ad_info.city;
      this.business_code = mapdata.ad_info.adcode;
      this.gotoCard();
    });
  },

  async submitCDKey() {
    if (!this.data.cdkey_no) return app.tips("请输入卡号");
    if (!this.data.cdkey_pwd) return app.tips("请输入卡密");

    let urlStr = app.getNetAddresss('plugin.business-district.frontend.controllers.cdkey.exchange');
    let response = await app._postNetWork({
      url: urlStr,
      data: {
        cdkey_no: this.data.cdkey_no,
        cdkey_pwd: this.data.cdkey_pwd,
      }
    });
    let { result, msg, data } = response.data;
    if(result == 0) return app.tips(msg);
    this.setData({
      addCDKeyPop: false
    });
    this.getData();
  },
  gotoCoupon(){
    let search_array = ['2'];
    wx.navigateTo({
      url: '/packageA/member/coupon_v2/coupon_v2?search_array=' + JSON.stringify(search_array)
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})